﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;

namespace Yxinmt.Controls
{
    public class Label : BaseControl, IControl
    {
        private System.Windows.Controls.Label _Label { get; set; }

        public Label(Dictionary<string, object> property, IComponent component, bool isFactory = false)
        {
            Property = property;
            _Component = component;
            _IsFactory = isFactory;
            if (_IsFactory)
            {
                _FactoryPropertyList = new List<FactoryProperty>();
                _ControlType = typeof(System.Windows.Controls.Label);
            }

            _Label = new System.Windows.Controls.Label();
            this.Element = _Label;

            bool blLabel = true;
            if (Property.ContainsKey("IsLabel")) blLabel = Property.GetValue<bool>("IsLabel");

            InitSet();
            SetProperty();
            SetControlProperty(blLabel);
        }

        private void SetProperty()
        {
            this.SetDefaultValue("IsDefaultValue", false);
            this.SetDefaultValue("Padding", "0,0,0,0");

            bool blDefaultValue = !Property.ContainsKey("IsDefaultValue") ? true : Property.GetValue<bool>("IsDefaultValue");

            if (blDefaultValue)
            {
                this.SetDefaultValue("TextAlign", "MiddleRight");
                this.SetDefaultValue("LabelMarginTop", 4);
                if (this.Property.ContainsKey("FontSize"))
                {
                    this.SetDefaultValue("Height", this.Property.GetValue<int>("FontSize") * 2);

                    string label = Property.GetStringValue("Label");
                    if (!string.IsNullOrEmpty(label)) this.SetDefaultValue("LabelWidth", this.Property.GetValue<int>("FontSize") * (label.Length + 1));
                }
            }
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.Label c = _Label;
            switch (name)
            {
                case "name": c.Name = string.Concat("lbl_", (string)value); SetFactoryProperty("Name", c.Name); break;
                case "Label": c.Content = (string)value; SetFactoryProperty("Content", c.Content); break;
                case "Text": c.Content = (string)value; SetFactoryProperty("Content", c.Content); break;
                case "LabelWidth": c.Width = Convert.ToDouble(value); SetFactoryProperty("Width", c.Width); break;
                case "Height": c.Height = Convert.ToDouble(value); SetFactoryProperty("Height", c.Height); break;
                case "BackColor": c.Background = GetColor((string)value); SetFactoryProperty("Background", c.Background); break;
                case "TextAlign": SetTextAlign((string)value); break;
                case "LabelTextAlign": SetTextAlign((string)value); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "LabelControlAlign": SetControlAlign((string)value); break;
                case "Padding": c.Padding = this.GetThickness((string)value); SetFactoryProperty("Padding", c.Padding); break;
                case "Margin": c.Margin = this.GetThickness((string)value); SetFactoryProperty("Margin", c.Margin); break;
                case "Visible": SetVisible(Convert.ToBoolean(value)); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
                case "BindingName": c.Content = new System.Windows.Data.Binding((string)value); SetFactoryProperty("Content", c.Content); break;
                case "Value": SetValue(value); break;
                case "Cursor": c.Cursor = GetCursor((string)value); break;
            }
        }

        protected override void SetFactoryProperty(string name, object value)
        {
            if (!_IsFactory) return;

            switch (name)
            {
                case "FontFamily": this.AddFactoryProperty(System.Windows.Controls.Label.FontFamilyProperty, value); break;
                case "FontSize": this.AddFactoryProperty(System.Windows.Controls.Label.FontSizeProperty, value); break;
                case "Foreground": this.AddFactoryProperty(System.Windows.Controls.Label.ForegroundProperty, value); break;
                case "Content": this.AddFactoryProperty(System.Windows.Controls.Label.ContentProperty, value); break;
                case "Margin": this.AddFactoryProperty(System.Windows.Controls.Label.MarginProperty, value); break;
                case "Name": this.AddFactoryProperty(System.Windows.Controls.Label.NameProperty, value); break;
                case "Height": this.AddFactoryProperty(System.Windows.Controls.Label.HeightProperty, value); break;
                case "Width": this.AddFactoryProperty(System.Windows.Controls.Label.WidthProperty, value); break;
                case "Padding": this.AddFactoryProperty(System.Windows.Controls.Label.PaddingProperty, value); break;
                case "VerticalAlignment": this.AddFactoryProperty(System.Windows.Controls.Label.VerticalAlignmentProperty, value); break;
                case "HorizontalAlignment": this.AddFactoryProperty(System.Windows.Controls.Label.HorizontalAlignmentProperty, value); break;
                case "VerticalContentAlignment": this.AddFactoryProperty(System.Windows.Controls.Label.VerticalContentAlignmentProperty, value); break;
                case "HorizontalContentAlignment": this.AddFactoryProperty(System.Windows.Controls.Label.HorizontalContentAlignmentProperty, value); break;
                case "Visibility": this.AddFactoryProperty(System.Windows.Controls.Label.VisibilityProperty, value); break;
                case "Background": this.AddFactoryProperty(System.Windows.Controls.Label.BackgroundProperty, value); break;
            }
        }

        public override void EventLoad()
        {
            bool isClick = this.Property.GetValue<bool>("IsClick");
            if (isClick) { this.BindClick(); this.ClickAction = (sender, e, ctl)=> this._Window.EventInvoke(sender, e, this); }
        }

        public override object GetValue()
        {
            return this._Label.Content;
        }

        public override void SetValue(object value)
        {
            _Label.Content = GetValueText(value);
        }

        public void SetFontColor(string color)
        {
            if (_Control == null) return;

            if (!string.IsNullOrEmpty(color))
            {
                this._Control.Foreground = new System.Windows.Media.SolidColorBrush(Utility.Common.GetColor(color));
            }
        }
    }
}
